home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / xlib50.zip / FLAT.INC < prev    next >
Text File  |  1995-02-15  |  18KB  |  408 lines

  1. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. ;+                                                                             +
  3. ;+                     FLAT.INC Version 1.0 Source Code                        +
  4. ;+                                                                             +
  5. ;+                Copyright 1995, by TechniLib (TM) Company                    +
  6. ;+                          All Rights Reserved                                +
  7. ;+                                                                             +
  8. ;+                  SALE OR USE OF SOFTWARE DEVELOPED WITH                     +
  9. ;+                     UNREGISTERED COPIES OF THIS CODE                        +
  10. ;+                           INFRINGES COPYRIGHT                               +
  11. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. ;                    Flat-Model Programming Under DOS
  14. ;                              Version 1.0
  15. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  16. ;   This include file may be used to enable approximate flat-model programming
  17. ;in assembly language under DOS.  The following template should be used in
  18. ;the program:
  19. ;
  20. ;              INCLUDE        FLAT.INC       ;Always 1st line
  21. ;
  22. ;MAIN          PROC NEAR                     ;Execution always begins at MAIN
  23. ;              .
  24. ;              .
  25. ;              .
  26. ;              RET                           ;Execution ends here
  27. ;MAIN          ENDP
  28. ;
  29. ;              [Other Procedures]            ;All procedures must be near
  30. ;
  31. ;              [Program Data]
  32. ;
  33. ;              END
  34. ;
  35. ;   The actual order of MAIN, other procedures, and data does not matter.
  36. ;The important things are that the module begin with FLAT.INC, that
  37. ;execution begin at MAIN, and that all procedures be near.
  38. ;   MAIN must be near for successful termination.  Other procedures must be
  39. ;near because the assembler will not properly handle far calls in protected
  40. ;mode.
  41. ;   The combined code and data may occupy more than 64K provided that:
  42. ;
  43. ;1) Real-mode code is never placed in the program.  This follows since real-
  44. ;mode will not be able to handle the large offsets in a 32-bit segment.
  45. ;2) Segment constants are never used in the program.  For example, never use
  46. ;a statement like MOV AX,DGROUP.  The DOS program loader will not be able
  47. ;to perform relocation edits upon such statements if the segment exceeds 64K.
  48. ;
  49. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  50. ;                          FLAT.INC Switches
  51. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  52. ;
  53. ;   There are five switches that affect the way FLAT.INC will assemble.  All
  54. ;of these switches default to FALSE.  To implement the switch, set it TRUE
  55. ;(TRUE = 1) before the inclusion of FLAT.INC
  56. ;
  57. ;1) TASMMODE:
  58. ;
  59. ;   IF you are a TASM programmer, then you should use the TASMMODE switch.
  60. ;The first two lines of your code would then be:
  61. ;
  62. ;TASMMODE      EQU            TRUE                          ;TRUE = 1
  63. ;              INCLUDE        FLAT.INC
  64. ;
  65. ;2) SUBMODULE:
  66. ;
  67. ;   If multiple modules are being used, then use the SUBMODULE switch in
  68. ;all modules apart from the main module.  The first two lines of code in
  69. ;submodules should be:
  70. ;
  71. ;SUBMODULE     EQU            TRUE                          ;TRUE = 1
  72. ;              INCLUDE        FLAT.INC
  73. ;
  74. ;3) ZEROBASE:
  75. ;
  76. ;   Under default behavior of FLAT.INC, MAIN will receive SS = TSEG selector.
  77. ;TSEG is the single 32-bit segment into which all 32-bit code and data are to
  78. ;reside.  ESP will be set for a stack of 4096 (1000H) free bytes.  MAIN will
  79. ;also receive DS = TSEG selector; ES = FLAT selector (zero base); FS = DSEG
  80. ;selector; GS = DGROUP selector.  All segments have 4Gb limits except FS and
  81. ;GS which have limits of 64K.  The advantages of these segment settings are
  82. ;discussed below.
  83. ;   In some cases one might prefer DS = FLAT and ES = TSEG.  A switch for
  84. ;FLAT.INC is provided for such cases.  It is called ZEROBASE.  ZEROBASE
  85. ;should be used for the main module only.
  86. ;
  87. ;4) VCPI:
  88. ;
  89. ;   If both DPMI and VCPI are present, then FLAT.INC configures for DPMI.
  90. ;If VCPI is preferred in such cases, then use the VCPI switch.  VCPI should
  91. ;be used for the main module only.
  92. ;
  93. ;5) NOTRAP
  94. ;
  95. ;   Use the NOTRAP switch to disable XLIB exception trapping.  NOTRAP should 
  96. ;be used for the main module only.
  97. ;
  98. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  99.  
  100. TRUE           EQU            1
  101. FALSE          EQU            0
  102.  
  103.                IFDEF TASMMODE
  104. DOTASM         =              TASMMODE
  105.                ELSE
  106. DOTASM         =              FALSE          ;Assemble for MASM
  107.                ENDIF
  108.  
  109.                IFDEF SUBMODULE
  110. DOSUBMODULE    =              SUBMODULE
  111.                ELSE
  112. DOSUBMODULE    =              FALSE          ;Current module assumed to be main
  113.                ENDIF
  114.  
  115.                IFDEF ZEROBASE
  116. DOZEROBASE     =              ZEROBASE
  117.                ELSE
  118. DOZEROBASE     =              FALSE          ;DS based at TSEG, ES based at zero
  119.                ENDIF
  120.  
  121.                IFDEF VCPI
  122. DOVCPI         =              VCPI
  123.                ELSE
  124. DOVCPI         =              FALSE          ;Default to DPMI
  125.                ENDIF
  126.  
  127.                IFDEF NOTRAP
  128. DONOTRAP       =              NOTRAP
  129.                ELSE
  130. DONOTRAP       =              FALSE          ;Default to exception trapping
  131.                ENDIF
  132.  
  133.                .MODEL         LARGE,PASCAL,FARSTACK
  134.                .386
  135.                .387
  136.  
  137.                IFE DOTASM
  138.  
  139.                INCLUDE        XLIB.INC
  140.                INCLUDELIB     XLIBE.LIB      ;Use XLIBE to trap exceptions
  141.  
  142.                ELSE
  143.  
  144.                MASM51
  145.                QUIRKS
  146.  
  147. PUSHW          MACRO IMMEDIATE16:REST        ;PUSH immediate 16-bit data
  148.                IF (@WordSize EQ 4)
  149.                DB             66H
  150.                ENDIF
  151.                DB             68H
  152.                DW             IMMEDIATE16
  153.                ENDM
  154.  
  155. PUSHD          MACRO IMMEDIATE32:REST        ;PUSH immediate 32-bit data
  156.                IF (@WordSize EQ 2)
  157.                DB             66H
  158.                ENDIF
  159.                DB             68H
  160.                DD             IMMEDIATE32
  161.                ENDM
  162.  
  163.                INCLUDE        XLIBB.INC
  164.                INCLUDELIB     XLIBEB.LIB     ;Use XLIBEB to trap exceptions
  165.  
  166.                ENDIF                         ;IFE DOTASM
  167.  
  168.                IFE DOSUBMODULE
  169.                .STACK         200H           ;Need a very small stack
  170.                .DATA
  171.                .CODE
  172.                .STARTUP
  173.  
  174. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  175. ;   The following instructions resize (reduce) the memory block containing the
  176. ;program.  Under default link options the program will consume all available
  177. ;memory.  This will cause XLIB to fail for inability to allocate memory for
  178. ;itself.
  179. ;   The code below assumes that the initial SS:SP points to the end of the
  180. ;program.  This will indeed be the case under both MASM and TASM if FARSTACK
  181. ;is used on the .MODEL statement.
  182. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  183.  
  184.                MOV            AX,SP          ;SS:SP = end of program
  185.                SHR            AX,4
  186.                MOV            BX,SS
  187.                ADD            BX,AX
  188.                INC            BX            ;BX = first para. beyond program
  189.                MOV            AX,ES         ;ES:0000 = first para. of program
  190.                SUB            BX,AX         ;BX = program size in para.
  191.                MOV            AH,4AH        ;Function to resize memory block
  192.                INT            21H           ;Carry will be set if failure
  193.                JNC            SHORT RESIZED
  194.                MOV            SI,OFFSET RESIZEFAIL
  195.                CALL           TELETYPE
  196.                MOV            AX,4C01H      ;Failed to resize so terminate
  197.                INT            21H
  198. RESIZED:
  199.  
  200. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  201. ;The following instructions initialize the XLIB DOS extender.
  202. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  203.  
  204.                PUSH           DSEG           ;Load XLIB data segment
  205.                POP            DS
  206.                ASSUME         DS:DSEG
  207.                FNINIT                        ;Initialize FPU
  208.                FLDCW          FPUCW          ;Load XLIB FPU control word
  209.                OR             OFLAGS,04H     ;Enable FPU save/restore
  210.                IF DOVCPI
  211.                OR             IFLAGS,01H
  212.                ENDIF
  213.                IF DONOTRAP
  214.                OR             IFLAGS,02H
  215.                ENDIF
  216.                CALL           INITXLIB       ;Initialize XLIB
  217.                OR             AX,AX          ;AX = 0 if success
  218.                JZ             SHORT INITDONE
  219.                MOV            SI,OFFSET INITFAIL
  220.                CALL           TELETYPE
  221.                MOV            CX,8
  222.                CALL           TELETYPEHEX    ;Print error code in EAX
  223.                MOV            SI,OFFSET NEWLINE
  224.                CALL           TELETYPE
  225.                MOV            AX,4C01H       ;Terminate
  226.                INT            21H
  227.  
  228. INITDONE:      XOR            EAX,EAX        ;Assume DS to be based at zero
  229.                IFE DOZEROBASE
  230.                MOV            AX,FLATDSEL    ;Set ES to FLAT
  231.                MOV            PMES,AX
  232.                MOV            AX,TSEGDSEL    ;Set DS to TSEG
  233.                MOV            PMDS,AX
  234.                MOV            AX,TSEG
  235.                ENDIF
  236.  
  237. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  238. ;   The following instructions set EBX such that DS:EBX will point to the
  239. ;program segment prefix as of entry to MAIN and EAX will contain the base
  240. ;address of DS.  EDX will contain the base address of TSEG.
  241. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  242.  
  243.                SHL            EAX,4          ;Compute base address for DS
  244.                XOR            EBX,EBX        ;Compute linear address of PSP
  245.                MOV            BX,ES
  246.                SHL            EBX,4
  247.                SUB            EBX,EAX        ;Normalize address to protected-mode DS
  248.                MOV            EDX,TSEG       ;Compute base address of 32-bit segment
  249.                SHL            EDX,4
  250.  
  251. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  252. ;   The follow instructions transfer execution to MAIN in 32-bit protected mode.
  253. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  254.  
  255.                PUSHD          OFFSET MAIN    ;Protected-mode target address
  256.                CALL           ENTERPM        ;Transfer control to target
  257.                MOV            AX,4C00H       ;Terminate upon return
  258.                INT            21H
  259.  
  260. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  261. ;Console routines and data for this include file.
  262. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  263.  
  264. ;Write string at CS:SI in teletype mode.
  265. TELETYPE       PROC NEAR
  266.                PUSHA
  267.                MOV            AH,0EH         ;Function number
  268.                MOV            BX,0007H       ;Zero page (BH), foreground = white (BL)
  269. CHARLOOP:      MOV            AL,CS:[SI]
  270.                INC            SI
  271.                OR             AL,AL
  272.                JZ             SHORT EXIT
  273. PRINTCHAR:     INT            10H
  274.                JMP            SHORT CHARLOOP
  275. EXIT:          POPA
  276.                RET
  277. TELETYPE       ENDP
  278.  
  279. ;Print EAX as hexadecimal in teletype mode.  Call with CX = number of hex
  280. ;digits to print.
  281. TELETYPEHEX    PROC NEAR
  282.                PUSHAD
  283.                MOV            BX,CX
  284.                MOV            EDX,EAX
  285. MAINLOOP:      AND            AL,0FH
  286.                ADD            AL,48
  287.                CMP            AL,57
  288.                JBE            SHORT PUSHCHAR
  289.                ADD            AL,7
  290. PUSHCHAR:      PUSH           AX
  291.                SHR            EDX,4
  292.                MOV            AL,DL
  293.                DEC            BX
  294.                JNZ            SHORT MAINLOOP
  295.                MOV            AH,0EH         ;Function number
  296.                MOV            BL,07H         ;Zero page (BH), foreground = white (BL)
  297. WRITELOOP:     POP            DX
  298.                MOV            AL,DL
  299.                INT            10H
  300.                DEC            CX
  301.                JNZ            SHORT WRITELOOP
  302.                POPAD
  303.                RET
  304. TELETYPEHEX    ENDP
  305.  
  306. RESIZEFAIL     DB "Error:  Failed to resize program memory block.",13,10,0
  307. INITFAIL       DB "XLIB initialization error: ",0
  308. NEWLINE        DB 13,10,0
  309.  
  310. @CurSeg        ENDS
  311.  
  312.                ENDIF                         ;IFE DOSUBMODULE
  313.  
  314. TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  315.                ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  316.  
  317.                IF DOTASM
  318.                LARGESTACK
  319.                ENDIF
  320.  
  321. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  322. ;                          Address Normalization
  323. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  324. ;
  325. ;   The default segment settings allow the programmer to write code that is
  326. ;very approximate to flat-model code.  In the true flat-model, all segments
  327. ;have base of zero  In the default FLAT.INC model, CS, SS, and DS have base
  328. ;equal to 16*TSEG.  Code for this model will differ from that of the true flat
  329. ;model only with references to data located outside of TSEG.  Offsets to such
  330. ;addresses must be normalized by subtracting the segment base address.  For
  331. ;example, the color screen would be accessed in the true flat model with
  332. ;offset B8000H.  In the approximate flat model the offset will be adjusted
  333. ;to B8000-16*TSEG.  The same normalization must be done to addresses returned
  334. ;by XLIB memory allocation routines.  The  following procedures are included
  335. ;to perform such normalization:
  336. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  337.  
  338.                IFE DOSUBMODULE
  339.  
  340. ;Allocate extended memory and return normalized address.  Call with EAX = size
  341. ;of block in bytes.  Error code returned in EAX.  Block handle returned in EBX.
  342. ;Number of allocated bytes returned in ECX.  Block is always at least as large
  343. ;as requested size.  Address of allocated block returned in EDX.  Address is
  344. ;always normalized to DS.  Call with EAX = 0 to get largest available block
  345. ;size in ECX.
  346. GETFLATMEM     PROC NEAR
  347.                IFE DOZEROBASE
  348.                PUSHD          OFFSET NORMADDRESS            ;PUSH return address
  349.                ENDIF
  350.                JMP            PMGETMEM
  351. GETFLATMEM     ENDP
  352.  
  353. ;Release previously allocated extended memory block.  Call with block handle
  354. ;in EAX.  Error code returned in EAX.  Extended memory is automatically freed
  355. ;upon termination.
  356. FREEFLATMEM    PROC NEAR
  357.                JMP            PMFREEMEM
  358. FREEFLATMEM    ENDP
  359.  
  360. ;Allocate DOS memory and return normalized address.  Call with EAX = size of
  361. ;block in bytes.  Error code returned in EAX.  Block handle returned in EBX.
  362. ;Number of allocated bytes returned in ECX.  Block is always at least as large
  363. ;as requested size.  Address of allocated block returned in EDX.  Address is
  364. ;always normalized to DS.  Call with EAX = 0 to get largest available block
  365. ;size in ECX.
  366. GETFLATDOSMEM  PROC NEAR
  367.                IFE DOZEROBASE
  368.                PUSHD          OFFSET NORMADDRESS            ;PUSH return address
  369.                ENDIF
  370.                JMP            PMGETDOSMEM
  371. GETFLATDOSMEM  ENDP
  372.  
  373. ;Release previously allocated DOS memory block.  Call with block handle in
  374. ;EAX.  Error code returned in EAX.  DOS memory is automatically freed upon
  375. ;termination.
  376. FREEFLATDOSMEM PROC NEAR
  377.                JMP            PMFREEDOSMEM
  378. FREEFLATDOSMEM ENDP
  379.  
  380.                IFE DOZEROBASE
  381.  
  382. ;Normalize logical address in EDX.
  383. NORMADDRESS    PROC NEAR
  384.                OR             EAX,EAX                       ;See if an error occurred
  385.                JNZ            SHORT EXIT
  386.                MOVZX          EAX,FS:TSEGVAL
  387.                SHL            EAX,4
  388.                SUB            EDX,EAX
  389.                XOR            EAX,EAX                       ;Set error code back to zero
  390. EXIT:          RET
  391. NORMADDRESS    ENDP
  392.  
  393.                ENDIF                                        ;IFE DOZEROBASE
  394.  
  395.                ENDIF                                        ;IFE DOSUBMODULE
  396.  
  397.                IFE DOTASM
  398.                EXTERNDEF PASCAL GETFLATMEM:NEAR
  399.                EXTERNDEF PASCAL GETFLATDOSMEM:NEAR
  400.                EXTERNDEF PASCAL FREEFLATMEM:NEAR
  401.                EXTERNDEF PASCAL FREEFLATDOSMEM:NEAR
  402.                ELSE
  403.                GLOBAL PASCAL GETFLATMEM:NEAR
  404.                GLOBAL PASCAL GETFLATDOSMEM:NEAR
  405.                GLOBAL PASCAL FREEFLATMEM:NEAR
  406.                GLOBAL PASCAL FREEFLATDOSMEM:NEAR
  407.                ENDIF
  408.